﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace frag_boardgame
{
    public class Logic
    {

        public bool CanMoveToSquare(Board board, Player player, int row, int column)
        {
            //checks if the square is out of bounds
            if (column < 0 || row < 0 || column >= board.Width || row >= board.Height)
                return false;

            int delta = Math.Abs(player.Row - row) + Math.Abs(player.Column - column);
            int movesLeft = player.MovesLeft;
            Square sqr = board.GetSquare(row, column);
            if ((delta > movesLeft) || (sqr.player != null))
                return false;
            else
                return true;
        }

        /// <summary>
        /// Checks if the given's player turn is over
        /// </summary>
        /// <param name="player"></param>
        /// <param name="delta"></param>
        /// <returns></returns>
        public bool isTurnOver(Player player)
        {

            return (player.MovesLeft == 0 && player.ShotsLeft == 0);
        }


        public bool canShoot(Player shooter, Player target)
        {
            return (shooter != target && shooter.ShotsLeft > 0);
        }

        /// <summary>
        /// Checks if the game is over
        /// </summary>
        /// <param name="pls"></param>
        /// <returns></returns>
        public bool isGameOver(ICollection<Player> pls)
        {
           
            if (pls.Count == 1)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// Get the winner from the player list
        /// </summary>
        /// <param name="pls"></param>
        /// <returns></returns>
        public Player getWinner(ICollection<Player> pls)
        {
            if (!isGameOver(pls)) throw new InvalidOperationException("Game is not over yet");
            return pls.ElementAt(0);
        }


        public bool canShoot(Player shooter, Player target, GameData gameData)
        {
            if (!canShoot(shooter,target))
                return false;
            
            Line lof = new Line(shooter.Column + 0.5, shooter.Row + 0.5
                , target.Column + 0.5, target.Row + 0.5);

            foreach (Player player in gameData.GetPlayersList())
            {
                if ((player.ID != shooter.ID) && (player.ID != target.ID))
                {
                    if (player.IsCrossing(lof))
                        return false;
                }
            }


            for (int r = 0; r < gameData.Board.Height; r++)
            {
                for (int c = 0; c < gameData.Board.Width; c++)
                {
                    if (gameData.Board.GetSquare(r, c).IsCrossing(lof))
                        return false;
                }
            }

            return true;
        
        }

        public bool CanMoveToSquare(Player player, int row, int column, GameData gameData)
        {
            if (!CanMoveToSquare(gameData.Board, player, row, column))
                return false;
                
            Line lom = new Line(player.Column + 0.5, player.Row + 0.5
                , column + 0.5, row + 0.5);


            for (int r = 0; r < gameData.Board.Height; r++)
            {
                for (int c = 0; c < gameData.Board.Width; c++)
                {
                    if (gameData.Board.GetSquare(r, c).IsCrossing(lom))
                        return false;
                }
            }

            return true;

        }

    
    }
        

        
}
